<?php
/**
 * @author jonathan <jo.link.noir@gmail.com>
 * @version 1.0: jo_link_noir
 * @date 19-06-2010
 */
class U1_Resource extends SW_Resource
{
	/** @var string */
	const CLASS_ELEMENT = 'U1_Resource_Element';

	/** @var string */
	const INDEX = 'resource';

	/**
	 * @param SW_Player $player
	 * @param int $idPlanet
	 */
	public function __construct(SW_Player $player, $idPlanet)
	{
		parent::__construct($player, $idPlanet, U1_Resource_Dependency::factory(self::INDEX), U1_Resource_DAO::getFactory(), self::CLASS_ELEMENT);
	}

	/**
	 * @param SW_Planet_Storage_Treatment $treatment
	 * @param array &$dependency
	 * @param int $asset_group
	 * @param int $race
	 * @return int|NULL
	 */
	static public function profitPerHour(SW_Planet_Storage_Treatment $treatment, &$dependency, $asset_group, $race)
	{
		$h = 0;
		foreach ($dependency as $type => &$groups)
		{
			$type = $treatment->getSiblingInstance($type);
			if (null === $type)
				return null;

			foreach ($groups as $group)
			{
				foreach ($type->getGroup($group) as $element)
				{
					$lvl = $element->getLevel();
					if (0 === $asset_group)
						$h += 10 + (4 + $lvl) * $lvl * 5.6;
					elseif (1 === $asset_group)
						$h += 10 + (3 + $lvl) * $lvl * 5.6;
					elseif (2 === $asset_group)
						$h += 1 + $lvl * $lvl * 5.6;
					elseif (3 === $asset_group)
						$h += pow($lvl, U1_Race::getIndex('ashen') === $race? 3 : 2) / 3 + sqrt($lvl) * 1.2;
					else
						return null;
				}
			}
		}

		if (U1_Race::getIndex('werlingz') === $race)
			return (int)($h * 1.2);
		elseif (U1_Race::getIndex('yukron') === $race)
			return (int)($h * 1.1);
		else
			return (int)$h;
	}

	/*
	 * Update de SplObserver, recalcule les ressources
	 * @param SW_I_Element_Subject $suject : SW_Structure_Element
	 */
// 	public function update(SW_I_Element_Subject $subject)
// 	{
// 		if ($subject instanceof U1_Building_Element && isset($this->elements[$subject->getId()]))
// 		{
// 			$element = $this->elements[$subject->getId()];
// 			$element->setTimeEnd($subject->getTimeEnd());
// 			$this->initPerHour($element);
// 			$this->dao->update($element);
// 		}
// 	}

	/**
	 * Crée une nouvelle ressource et la sauvegarde. Si $quantitée vaut 0 alors une par défaut lui sera affectée
	 * @param int $id_structure
	 * @param int $group
	 * @param int $level
	 * @param int $quantity = 0
	 * @param int $time_end = null ($_SERVER['REQUEST_TIME'])
	 */
	public function createElement($id_structure, $group, $quantity=0, $time_end=null)
	{
		parent::createElement($id_structure, $group,
			(!$quantity)
				? ((0 === $group || 1 === $group)
					? 5000
					: ((2 === $group)
						? 1000
						: 0
				)) : $quantity,
			$time_end
		);
	}

	public function __toString()
	{
		return self::INDEX;
	}
}